﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.Admin
{
    public partial class BookEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string productID = Request.QueryString["productid"];

                ProductsBusiness pb = new ProductsBusiness();
                DataTable dt = pb.GetDataDetails(productID);

                txtName.Text = dt.Rows[0]["productname"].ToString();
                txtDes.Text = dt.Rows[0]["description"].ToString();
                txtPrice.Text = dt.Rows[0]["price"].ToString();
                txtStock.Text = dt.Rows[0]["stockquantity"].ToString();
                txtSales.Text = dt.Rows[0]["salesquantity"].ToString();
                Image1.ImageUrl = dt.Rows[0]["image"].ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string fileName = Path.GetFileName(FileUpload1.FileName);
                string fileExtension = Path.GetExtension(fileName).ToLower();
                string[] allowedExtensions = { ".jpg", ".jpeg", ".png", ".gif" };

                if (allowedExtensions.Contains(fileExtension))
                {
                    string path = Server.MapPath("~/assert/img/");
                    path += fileName;
                    FileUpload1.SaveAs(path);
                    Image1.ImageUrl = "~/assert/img/" + fileName;
                }
                else
                {
                    Response.Write("<script>alert('请选择正确的图片文件格式！')</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('请选择文件！')</script>");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ProductsEntity pe = new ProductsEntity();
            pe.Productname = txtName.Text;
            pe.Description = txtDes.Text;
            pe.Price = Convert.ToDecimal(txtPrice.Text);
            pe.Stockquantity = Convert.ToInt32(txtStock.Text);
            pe.Salesquantity = Convert.ToInt32(txtSales.Text);
            pe.Image = Image1.ImageUrl;

            ProductsBusiness pb = new ProductsBusiness();
            int res = pb.EditProduct(pe);

            if (res > 0)
            {
                Response.Write("<script>alert('更新成功')</script>");
                Response.Redirect("BookManage.aspx");
            }
            else
            {
                Response.Write("<script>alert('更新失败')</script>");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("BookManage.aspx");
        }
    }
}